package com.lenovo.smarttraffic.zm_user_module;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CollectionUserBean;
import com.lenovo.smarttraffic.database.dao.CollectionUserBeanDao;
import com.lenovo.smarttraffic.database.dao.DaoManager_Collection;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.util.List;

public class CollectionActivity extends BaseActivity {

    private RecyclerView rv_collection;
    private List<CollectionUserBean> list;
    private Collection_Adapter collection_adapter;
    private CollectionUserBeanDao collectionUserBeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "用户收藏");
        rv_collection = (RecyclerView) findViewById(R.id.rv_collection);
        DaoManager_Collection.getInstance_Collection().init(this);
        collectionUserBeanDao = DaoManager_Collection.getInstance_Collection().getDaoSession_Collection().getCollectionUserBeanDao();
        list = collectionUserBeanDao.queryBuilder().list();
        collection_adapter = new Collection_Adapter(this, list);
        rv_collection.setAdapter(collection_adapter);
        rv_collection.setLayoutManager(new LinearLayoutManager(this));

        collection_adapter.setOnLis(new Collection_Adapter.OnLis() {
            @Override
            public void onTop(int position, DragLayout dragLayout) {
                dragLayout.setStatus(DragLayout.Status.Close, true);
                CollectionUserBean zhiding = list.get(position);
                list.remove(position);
//                collecction_adapter.notifyItemRemoved(position);
//                collecction_adapter.notifyItemRangeChanged(0, list.size() - position);
                zhiding.setZhiding(true);
                list.add(0, zhiding);
                collection_adapter.notifyDataSetChanged();
                rv_collection.setAdapter(collection_adapter);
                rv_collection.setLayoutManager(new LinearLayoutManager(CollectionActivity.this));
//                collecction_adapter.notifyItemInserted(0);
//                collecction_adapter.notifyItemRangeChanged(0, list.size() - 0);
                Toast.makeText(getApplicationContext(), "置顶", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void insert(int position, DragLayout dragLayout) {

            }

            @Override
            public void delete(int position, DragLayout dragLayout) {

                collectionUserBeanDao.delete(list.get(position));
                list.remove(position);
                collection_adapter.notifyDataSetChanged();
                rv_collection.setAdapter(collection_adapter);
                rv_collection.setLayoutManager(new LinearLayoutManager(CollectionActivity.this));
                Toast.makeText(CollectionActivity.this, "asdfghjk" + collectionUserBeanDao.queryBuilder().list().size(), Toast.LENGTH_SHORT).show();


            }

            @Override
            public void untop(int position, DragLayout dragLayout) {
                dragLayout.setStatus(DragLayout.Status.Close, true);
                CollectionUserBean zhiding = list.get(position);
                list.remove(position);
                zhiding.setZhiding(false);
                list.add(zhiding);
                collection_adapter.notifyDataSetChanged();
                rv_collection.setAdapter(collection_adapter);
                rv_collection.setLayoutManager(new LinearLayoutManager(CollectionActivity.this));
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_collection;
    }
}
